<?php
/**
 * Created by PhpStorm.
 * User: Chris
 * Date: 14/12/14
 * Time: 下午8:31
 */

class Controller_Mobile_Register extends Controller_Mobile_Base{




    /**
     * 注册
     */
    public function action_index(){
//        $industryList = Model_Industry::find('all');
        $userInfoResponse = Session::get(SESSION_WEIXION);
        if ($userInfoResponse){
            $data['weixinUserInfo'] = $userInfoResponse;
            // check if the weixin has been register before?!!!
            $existChannel = Model_UserChannel::findByAuthIdNChannel($userInfoResponse->openid,
                Model_UserChannel::CHANNEL_WEIXIN);
            if ($existChannel){
//                Response::redirect(Uri::create('mobile/user/card'));
                Response::redirect(Uri::create('mobile/'));
                return;
            }
        }

        if (Input::method()=='POST'){
            $name = Input::param('name');
//            $password = Input::param('name');
            $wx_id = Input::param('wx_id');
            $password = Input::param('password');
            $mobile = Input::param('mobile');
            $email = Input::param('email');
            $company = Input::param('company');
            $department = Input::param('department');
            $job_title = Input::param('job_title');
            $location_id = Input::param('location_id');
//            $gender = Input::param('gender');
            $remark = Input::param('remark');
            $regiser_ip = Input::real_ip();;

            try {

                $user = UserService::register(
                    $regiser_ip, $name, $company,
                    $department, $job_title, $wx_id, $password, $mobile, $email,
                    $location_id, $remark, $userInfoResponse);
                $this->user_login($user->id, $user->name);
                Session::set_flash("success", "注册成功");
                $nextUrl = Input::param('nextUrl', '');
                if ($nextUrl==''){
                    $nextUrl = Uri::create('mobile/user');
                }
                Response::redirect($nextUrl);
                return;
            }catch (Exception $e){
                if ($e->getCode()==23000){
//                    Session::set_flash("error", "注册失败, 手机号重复");
                    Session::set_flash("error", "注册失败, ".$e->getMessage());
                }else{
//                    var_dump($e);exit;
                    Session::set_flash("error", "注册失败, ".$e->getMessage());
                }
            }
        }


        $data = array();
//        $data['industryList'] = $industryList;
        $this->template->content = View::forge('mobile/register', $data);
    }

}